<template>
  <div id="app">
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>飞机列表</span>
              <el-button type="primary" @click="handleAddPlane">添加飞机</el-button>
            </div>
          </template>
          <el-table :data="planes" border style="width: 100%">
            <el-table-column prop="id" label="飞机编号">
            </el-table-column>
            <el-table-column prop="name" label="飞机名称">
            </el-table-column>
            <el-table-column prop="type" label="飞机类型">
            </el-table-column>
            <el-table-column label="舱段名称">
              <template #default="scope">
                <div v-for="cabin in scope.row.cabins" :key="cabin.id">
                  {{ cabin.name }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="舱段类型">
              <template #default="scope">
                <div v-for="cabin in scope.row.cabins" :key="cabin.id">
                  {{ cabin.type }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="舱段容量">
              <template #default="scope">
                <div v-for="cabin in scope.row.cabins" :key="cabin.id">
                  {{ cabin.capacity }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="180">
              <template #default="scope">
                <el-button type="primary" size="small" @click="handleEditPlane(scope.row)">编辑</el-button>
                <el-button type="danger" size="small" @click="handleDeletePlane(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog v-model="addPlaneDialogVisible" title="添加飞机" width="30%">
      <el-form :model="form">
        <el-form-item label="飞机名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="飞机类型">
          <el-input v-model="form.type" />
        </el-form-item>
        <div style="margin-top:20px" v-for="(cabin, index) in cabins" :key="index">
          <el-input v-model="cabin.name" placeholder="舱段名"></el-input>
          <el-input v-model="cabin.type" placeholder="舱段类型"></el-input>
          <el-input v-model="cabin.capacity" placeholder="舱段容量" type="number"></el-input>
          <el-button type="danger" @click="removeCabin(index,cabins)">删除</el-button>
        </div>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addPlaneDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addCabin(cabins)">添加舱段</el-button>
          <el-button type="primary" @click="handleSavePlane">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog v-model="updatePlaneDialogVisible" title="更新飞机" width="50%">
      <span>请注意：已投入航班中使用的飞机不允许临时修改舱段信息，请到对应航次进行修改</span>
      <el-form :model="updatePlane">
        <el-form-item label="飞机编号">
          <el-input disabled v-model="updatePlane.id" />
        </el-form-item>
        <el-form-item label="飞机名称">
          <el-input v-model="updatePlane.name" />
        </el-form-item>
        <el-form-item label="飞机类型">
          <el-input v-model="updatePlane.type" />
        </el-form-item>
        <div style="margin-top:20px" v-for="(cabin, index) in updatePlane.cabins" :key="index">
          <el-input v-model="cabin.name" placeholder="舱段名"></el-input>
          <el-input v-model="cabin.type" placeholder="舱段类型"></el-input>
          <el-input v-model="cabin.capacity" placeholder="舱段容量" type="number"></el-input>
          <el-button type="danger" @click="removeCabin(index,updatePlane.cabins)">删除</el-button>
        </div>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <span>要点击确定按钮才会保存修改哦~</span>
          <el-button @click="updatePlaneDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addCabin(updatePlane.cabins)">添加舱段</el-button>
          <el-button type="primary" @click="handleUpdatePlane">确定</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog v-model="deletePlaneDialogVisible" title="删除飞机" width="50%">
      <span>请注意：删除飞机后所有相关航班信息都会被删除，请谨慎操作</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="deletePlaneDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleDeletePlaneReally()">确定删除</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  name: 'AdminPlane',
  data() {
    return {
      cabins: [],
      planes: [],
      addPlaneDialogVisible: false,
      updatePlaneDialogVisible: false,
      deletePlaneDialogVisible: false,
      form: {
        name: '',
        type:'',
      },
      updatePlane: {
        id: '',
        name: '',
        type: '',
        cabins: [],
      },
      deletePlaneId: '',
    };
  },
  mounted() {
    // this.fetchCabins();
    this.fetchPlanes();
  },
  methods: {
    addCabin(cabins) {
      cabins.push({ name: '', type: '', capacity: ""});
    },
    removeCabin(index,cabins) {
      cabins.splice(index, 1);
    },
    fetchCabins() {
      axios.get('/cabins')
          .then(response => {
            this.cabins = response.data;
          })
          .catch(error => {
            console.error('获取舱段列表失败', error);
            ElMessage.error('获取舱段列表失败');
          });
    },
    fetchPlanes() {
      axios.get('/planes')
          .then(response => {
            this.planes = response.data;
          })
          .catch(error => {
            console.error('获取飞机列表失败', error);
            ElMessage.error('获取飞机列表失败');
          });
    },
    handleAddPlane() {
      this.addPlaneDialogVisible = true;
      this.form = {
        cityName: '',
        provinceCode: null,
      };
    },
    handleSavePlane() {
      const PlaneDTO = {
        name: this.form.name,
        type: this.form.type,
        cabins: this.cabins,
      };
      axios.post('/plane', PlaneDTO)
          .then(() => {
            this.addPlaneDialogVisible = false;
            this.fetchPlanes();
            ElMessage.success('添加飞机成功');
          })
          .catch(error => {
            console.error('添加飞机失败', error);
            ElMessage.error('添加飞机失败');
          });
    },
    handleUpdatePlane(){
      console.log(this.updatePlane);
      const PlaneDTO = {
        name: this.updatePlane.name,
        type: this.updatePlane.type,
        cabins: this.updatePlane.cabins,
      };
      axios.put(`/plane/${this.updatePlane.id}`, PlaneDTO)
          .then(() => {
            this.updatePlaneDialogVisible = false;
            this.fetchPlanes();
            ElMessage.success('更新飞机成功');
          })
          .catch(error => {
            console.error('更新飞机失败', error);
            ElMessage.error('您修改的数据涉及已经投入使用的航班，请先删除相关联的航班再进行操作');
          });
    },
    handleEditPlane(plane) {
      this.updatePlane = plane;
      this.updatePlaneDialogVisible = true;
      console.log('编辑飞机',  plane);
    },
    handleDeletePlane(plane) {
      this.deletePlaneDialogVisible = true;
      this.deletePlaneId = plane.id;
      console.log('删除飞机',  plane);
    },
    handleDeletePlaneReally(){
      axios.delete(`/plane/${this.deletePlaneId}`)
          .then(() => {
            this.deletePlaneDialogVisible = false;
            this.fetchPlanes();
            ElMessage.success('删除飞机成功');
          })
          .catch(error => {
            console.error('删除飞机失败', error);
            ElMessage.error('删除飞机失败');
          });
    },
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.box-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
